package top.itaso.app.ui.settings

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import top.itaso.app.R
import top.itaso.app.common.SPUtils
import top.itaso.lib.common.FileUtils
import top.itaso.lib.common.ReplaceWithStarUtils
import top.itaso.lib.common.ToastUtils
import top.itaso.lib.widget.CommonDialog
import top.itaso.lib.widget.tpv.TpvCommonItemView

class SettingsFragment : Fragment() {
    private lateinit var mViewModel: SettingsViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewModel = ViewModelProvider(this).get(SettingsViewModel::class.java)
        val rootView = inflater.inflate(R.layout.fragment_settings, container, false)
        initView(rootView)
        return rootView
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
    }

    private fun initView(rootView: View) {
        val layoutuserInfoItem: TpvCommonItemView = rootView.findViewById(R.id.layout_info)
        layoutuserInfoItem.setMarkContent(mViewModel.loginUserName)
        layoutuserInfoItem.setOnClickListener {

        }

        val layoutAboutItem: TpvCommonItemView = rootView.findViewById(R.id.layout_about)
        layoutAboutItem.setMarkContent(mViewModel.appVersion)
        layoutAboutItem.setOnClickListener {
            Log.d(TAG, "initView: >>>>>>")
            FileUtils.readLineFromFile("/storage/emulated/0/abcd.docx")
        }

        val layoutContactItem: TpvCommonItemView = rootView.findViewById(R.id.layout_contact)
        layoutContactItem.setMarkContent(ReplaceWithStarUtils.replacePhone2Star("15866666680"))
        layoutContactItem.setOnClickListener {

        }

        val layoutProtectItem: TpvCommonItemView = rootView.findViewById(R.id.layout_protect)
        layoutProtectItem.switchStatus = SPUtils.getInstance().readIsProtect()
        layoutProtectItem.setItemOnClickListener {
            //TODO 此处后续将添加密码校验功能
            val isOpen = SPUtils.getInstance().readIsProtect()

            val rootView = LayoutInflater.from(context).inflate(R.layout.layout_pwd_setting, null)
            rootView.setBackgroundColor(resources.getColor(R.color.transparent))
            val etPwdInput = rootView.findViewById<EditText>(R.id.et_input_password)
            val tvPwdTip = rootView.findViewById<TextView>(R.id.tv_pwd_input_err_tip)
            val dialog = if (isOpen) {
                CommonDialog.Builder(context, CommonDialog.EDialogDefined.whole)
                    .setTitle("关闭密码")
                    .setContentView(rootView)
                    .setPositiveClickListener("关闭", object : CommonDialog.IPositiveClickListener() {

                        override fun onClick() {
                            val pwdValue = etPwdInput.text.toString()
                            val curPwdValue = SPUtils.getInstance()
                                .getValue(SPUtils.SPKey.SP_KEY_PROTECT_PWD, String::class.java)
                            Log.d(
                                TAG,
                                "initView: >>>>>>pwdValue=$pwdValue,curPwdValue=$curPwdValue"
                            )
                            if (TextUtils.isEmpty(pwdValue) || pwdValue != curPwdValue) {
                                tvPwdTip.visibility = View.VISIBLE
                                tvPwdTip.text = resources.getString(R.string.tip_pwd_error)
                                setDismiss(false)
                                return
                            } else {
                                tvPwdTip.visibility = View.GONE
                            }
                            SPUtils.getInstance().remove(SPUtils.SPKey.SP_KEY_PROTECT_PWD)
                            layoutProtectItem.switchStatus = false
                        }

                    })
                    .setNegativeClickListener(
                        "取消"
                    ) { }
                    .build()
            } else {
                CommonDialog.Builder(context, CommonDialog.EDialogDefined.whole)
                    .setTitle("设置密码")
                    .setContentView(rootView)
                    .setPositiveClickListener(
                        "保存", object : CommonDialog.IPositiveClickListener() {
                            override fun onClick() {
                                val pwdValue = etPwdInput.text.toString()
                                Log.d(TAG, "initView: >>>>>>pwdValue=$pwdValue")
                                if (TextUtils.isEmpty(pwdValue) || pwdValue.length < 4) {
                                    tvPwdTip.visibility = View.VISIBLE
                                    tvPwdTip.text = resources.getString(R.string.tip_pwd_illegal)
                                    setDismiss(false)
                                    return
                                } else {
                                    tvPwdTip.visibility = View.GONE
                                }

                                SPUtils.getInstance()
                                    .saveValue(SPUtils.SPKey.SP_KEY_PROTECT_PWD, pwdValue)
                                layoutProtectItem.switchStatus = true
                            }

                        }
                    )
                    .setNegativeClickListener(
                        "取消"
                    ) { }
                    .setNeutralClickListener(
                        "忽略"
                    ) { }
                    .build()
            }
            dialog.show()
        }
        layoutProtectItem.setItemOnCheckedChangeListener { compoundButton, isChecked ->
            Log.d(TAG, "initView: >>>>>>PROTECT_STATUS=$isChecked")
            SPUtils.getInstance().saveBoolean(SPUtils.SPKey.SP_KEY_PROTECT_STATUS, isChecked)
        }

        val layoutShowInfoItem: TpvCommonItemView = rootView.findViewById(R.id.layout_showinfo)
        layoutShowInfoItem.setOnClickListener {
//            NavController navController = NavHostFragment . findNavController (CasesFragment.this);
            val navController = Navigation.findNavController(
                requireActivity(),
                R.id.nav_host_fragment_content_main_drawer
            )
            navController.navigate(R.id.nav_showinfo)
        }

        val loginOut = rootView.findViewById<TextView>(R.id.tv_logout)
        loginOut.isSelected = true
        loginOut.setOnClickListener {
            val dialog = CommonDialog.Builder(context, CommonDialog.EDialogDefined.original)
                .setTitle("警告")
                .setMessage("确认要退出账号吗？")
                .setPositiveClickListener(
                    "确认", object : CommonDialog.IPositiveClickListener() {
                        override fun onClick() {
                            val isLogout = mViewModel.onLogout()
                            if (isLogout) {
                                ToastUtils.getInstance().showLongToast(context, "退出成功！")
                            } else {
                                ToastUtils.getInstance().showLongToast(context, "退出失败！")
                            }
                        }

                    }
                )
                .setNegativeClickListener(
                    "在想想"
                ) { }
                .setNeutralClickListener(
                    "忽略"
                ) { }
                .build()

            dialog.show()
        }
    }

    companion object {
        fun newInstance(): SettingsFragment {
            return SettingsFragment()
        }

        private const val TAG = "SettingsFragment"
    }
}